<?php

namespace App\Http\Controllers\admin;

use Illuminate\Http\Request;

use App\Http\Controllers\Controller;

use App\Http\Requests;

use DB;

class AddressController extends Controller
{
    //地址
    public function index(Request $request)
    {
        // 搜索分页
        $where = [];
        $db = DB::table('storeaddress')
            ->join('storeuser', 'storeuser.userId', '=', 'storeaddress.uid')
            ->select('storeaddress.*', 'storeuser.userName','storeuser.userId')
            ->orderBy('storeuser.userName');
        
        if($request->has('userName')){
            $db->where('userName','like',"%{$request->input('userName')}%");
            $where['userName'] = $request->input('userName');
        }
        $list = $db->paginate(3);
        return view('admin.Address',['list'=>$list,'where'=>$where]);
    }

    public function create()
    {
        return view('admin.addressAdd');
    }

    public function store(Request $request)
    {
        // 获取与数据库相同的字段
        $province = $request['city1'];
        $city = $request['city2'];
        $country = $request['city3'];
        $town = $request['city4'];

        // 得到省名称
        $p = DB::table('district')
            ->select('name')
            ->where('id','=',$province)
            ->get();
         foreach ($p as $key => $value) {
            $pp = $value;
            foreach ($pp as $key => $v) {
                $province = $v;
            }
        }

        // 得到市名称
        $c = DB::table('district')
            ->select('name')
            ->where('id','=',$city)
            ->get();
         foreach ($c as $key => $value) {
            $cc = $value;
            foreach ($cc as $key => $v) {
                $city = $v;
            }
        }
        
        // 得到县名称
        $cc = DB::table('district')
            ->select('name')
            ->where('id','=',$country)
            ->get();
        foreach ($cc as $key => $value) {
            $ccc = $value;
            foreach ($ccc as $key => $v) {
                $country = $v;
            }
        }

        // 得到镇名称
        $t = DB::table('district')
            ->select('name')
            ->where('id','=',$town)
            ->get();
        foreach ($t as $key => $value) {
            $tt = $value;
            foreach ($tt as $key => $v) {
                $town = $v;
            }
        }

        // 去除token以及不需要的字段
        $data = $request->except('_token','city1','city2','city3','city4');

        // 得到数据库相对应的省市县镇
        $data['province'] = $province;
        $data['city'] = $city;

        // 如果等于---请选择---时为Null
        if ($country == '---请选择---') {
            $data['country'] = '';
        }else{
            $data['country'] = $country;
        }

        if ($town== '---请选择---') {
            $data['town'] = '';
        }else{
            $data['town'] = $town;
        }


        //自定义错误信息
        $message = [
            'required' => ':attribute 字段值是必填项',
            'digits' => ':attribute 字段值必须为数值且长度为11',
        ];

        //设定验证规则
        $this->validate($request, [
            'consigneePhone' => 'required|digits:11',
            'detailAddress' => 'required',
            'consignee' => 'required',
        ],$message);

        // 执行插入
        $row = DB::table('storeaddress')->insert($data);
        if($row>0){
            return redirect('/admin/Address')->with('msg','添加成功');
        }
    }

    public function edit($id)
    {
        $value = DB::table('storeaddress')->where('id',$id)->first();
        return view('admin.addressEdit',['v'=>$value]);
    }

    public function update($id,Request $request)
    {
        // 同添加，得到数据库所对应的省市县镇
        $un = DB::table('storeaddress')
            ->join('storeuser','storeaddress.uid','=','storeuser.userId')
            ->select('storeaddress.id')
            ->where('storeaddress.uid',$id)
            ->get();

        foreach ($un as $key => $value) {
            $userN = $value;
            foreach ($userN as $key => $v) {
                $userName = $v;
            }
        }

        $province = $request['city1'];
        $city = $request['city2'];
        $country = $request['city3'];
        $town = $request['city4'];

        $p = DB::table('district')
            ->select('name')
            ->where('id','=',$province)
            ->get();
         foreach ($p as $key => $value) {
            $pp = $value;
            foreach ($pp as $key => $v) {
                $province = $v;
            }
        }

        $c = DB::table('district')
            ->select('name')
            ->where('id','=',$city)
            ->get();
         foreach ($c as $key => $value) {
            $cc = $value;
            foreach ($cc as $key => $v) {
                $city = $v;
            }
        }

        $cc = DB::table('district')
            ->select('name')
            ->where('id','=',$country)
            ->get();
        foreach ($cc as $key => $value) {
            $ccc = $value;
            foreach ($ccc as $key => $v) {
                $country = $v;
            }
        }

        $t = DB::table('district')
            ->select('name')
            ->where('id','=',$town)
            ->get();
        foreach ($t as $key => $value) {
            $tt = $value;
            foreach ($tt as $key => $v) {
                $town = $v;
            }
        }

        $data = $request->except('_token','city1','city2','city3','city4','_method');
         
        $data['province'] = $province;
        $data['city'] = $city;

        if ($country == '---请选择---') {
            $data['country'] = '';
        }else{
            $data['country'] = $country;
        }

        if ($town== '---请选择---') {
            $data['town'] = '';
        }else{
            $data['town'] = $town;
        }
        //自定义错误信息
        $message = [
            'required' => ':attribute 字段值是必填项',
            'digits' => ':attribute 字段值必须为数值且长度为11',
        ];
        //设定验证规则
        $this->validate($request, [
            'consigneePhone' => 'required|digits:11',
            'detailAddress' => 'required',
            'consignee' => 'required',
        ],$message);
        
        $row = DB::table('storeaddress')->where('id',$id)->update($data);
        if($row>0){
            return redirect('/admin/Address')->with('msg','修改成功');
        }
    }

    public function destroy($id)
    {
        $row = DB::table('storeaddress')->where('id',$id)->delete();
        if($row>0){
            return redirect('/admin/Address')->with('msg','删除成功');
        }
    }

}